package com.example.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.example.product.entity.AttrAttrgroupRelationEntity;
import com.example.product.entity.AttrEntity;
import com.example.product.service.AttrAttrgroupRelationService;
import com.example.product.service.AttrService;
import com.example.product.service.CategoryService;
import com.example.product.vo.AttrGroupWithAttrRespVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.product.entity.AttrGroupEntity;
import com.example.product.service.AttrGroupService;
import com.example.common.utils.PageUtils;
import com.example.common.utils.R;



/**
 * 属性分组
 *
 * @author cheval
 * @email chevalyang@163.com
 * @date 2022-12-28 23:23:09
 */
@Slf4j
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    // @RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable Long catId){
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    // @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
		if (attrGroup != null) {
            Long[] catePath = categoryService.getCategoryPathById(attrGroup.getCatelogId());
            attrGroup.setCatelogPath(catePath);
        }
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }


    /**
     * 获取分组关联的属性集合
     * @param attrGroupId
     * @return
     */
    @RequestMapping("/{attrGroupId}/attr/relation")
    public R getAttrGroupRelationAttr(@PathVariable Long attrGroupId) {
        List<AttrEntity> attrEntityList =  attrGroupService.queryAttrGroupRelationAttrs(attrGroupId);
        return R.ok().put("data",attrEntityList);
    }

    /**
     * 批量删除分组关联的属性
     * @param entities
     * @return
     */
    @PostMapping("/attr/relation/delete")
    public R removeAttrGroupRelationAttr(@RequestBody AttrAttrgroupRelationEntity[] entities) {
        attrGroupService.removeAttrGroupRelationAttr(entities);
        return R.ok();
    }

    /**
     * 获取分组未关联的属性
     * 1. 分组未关联的
     * 2. 当前分组所属分类下的基本属性
     * 3. 没有被其他分组关联的
     * @param params
     * @param attrGroupId
     * @return
     */
    @RequestMapping("/{attrGroupId}/noattr/relation")
    public R getGroupNoRelationAttr(@RequestParam Map<String, Object> params,@PathVariable("attrGroupId") Long attrGroupId) {
        PageUtils page =  attrService.getGroupNoRelationAttr(params,attrGroupId);
        return R.ok().put("page",page);
    }

    /**
     * 给分组添加关联属性
     * @param entities
     * @return
     */
    @RequestMapping("/attr/relation")
    public R saveGroupRelationAttr(@RequestBody AttrAttrgroupRelationEntity[] entities) {
        if (entities != null && entities.length > 0) {
            attrAttrgroupRelationService.saveBatch(Arrays.asList(entities));
        }
        return R.ok();
    }

    /**
     * 查询分类下的所有属性组以及属性组关联的属性
     * @param catelogId
     * @return
     */
    @RequestMapping("/{catelogId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable Long catelogId) {
        List<AttrGroupWithAttrRespVo> attrGroupWithAttrRespVos = attrGroupService.getAttrGroupWithAttrs(catelogId);
        return R.ok().put("data",attrGroupWithAttrRespVos);
    }

}
